package com.yonhee.datastructure.linkedList;


import java.util.Iterator;
import java.util.function.Consumer;

/**
 * 单项链表
 */
public class SinglyLinkedList implements Iterable<Integer> {  // 链表整体
    private Node head = null; // 头指针

    /**
     * 添加到首部
     *
     * @param element
     */
    public void addFront(int element) {
        // 1.链表为空
//        head = new Node(element, null);

        // 2.链表不为空
        head = new Node(element, head);
    }

    /**
     * 添加到尾部
     *
     * @param element
     */
    public void addBack(int element) {
        Node lastNode = this.findLast();
        if (lastNode != null) {
            lastNode.next = new Node(element, null);
        } else {
            this.addFront(element);
        }
    }

    /**
     * 获取最后一个节点
     * @return
     */
    private Node findLast() {
        if (head == null) {
            return null;
        }

        Node p = head;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

    /**
     * 获取指定索引位置的值
     *
     * @param index 索引
     */
    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException("index is illegal");
        }
        return node.value;
    }

    /**
     * 获取指定索引位置的节点
     *
     * @param index 索引
     */
    private Node findNode(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    /**
     * 向指定索引位置插入节点
     *
     * @param index
     * @param element
     */
    public void add(int index, int element) {
        if (index == 0) {
            this.addFront(element);
            return;
        }
        Node prev = this.findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException("index is illegal");
        }
        prev.next = new Node(element, prev.next);

    }

    /**
     * 删除首元素
     */
    public void removeFront() {
        if (head == null) {
            throw new IllegalArgumentException("list is empty");
        }
        head = head.next;
    }

    /**
     * 删除指定索引位置的元素
     * @param index
     */
    public void remove(int index) {
        if (index == 0) {
            this.removeFront();
            return;
        }
        Node prev = this.findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException("index is illegal");
        }
        Node removed = prev.next;
        if (removed == null) {
            throw new IllegalArgumentException("index is illegal");
        }
        prev.next = removed.next;
    }

    /**
     * 遍历
     */
    public void loop() {
        Node pointer = head;
        while (pointer != null) {
            System.out.println(pointer.value);
            pointer = pointer.next;
        }
    }

    /**
     * 遍历
     */
    public void loop2while(Consumer<Integer> consumer) {
        Node pointer = head;
        while (pointer != null) {
            consumer.accept(pointer.value);
            pointer = pointer.next;
        }
    }


    /**
     * 遍历
     */
    public void loop2for(Consumer<Integer> consumer) {
        for (Node pointer = head; pointer != null; pointer = pointer.next) {
            consumer.accept(pointer.value);
        }
    }

    /**
     * 递归遍历
     */
    public void loop2recursion() {
        recursion(head);
    }

    /**
     * 对某个节点要进行的操作
     * @param cur 当前要操作的节点
     */
    private void recursion(Node cur) {
        if (cur == null) {
            return;
        }
        System.out.println(cur.value);
        recursion(cur.next);
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {

            private Node pointer = head;

            @Override
            public boolean hasNext() {
                return pointer != null;
            }

            @Override
            public Integer next() {
                Integer result = pointer.value;
                pointer = pointer.next;
                return result;
            }
        };
    }

    /**
     * 节点类(内部类: 对外隐藏实现细节)
     * static: 当内部类中使用了外部类的成员变量的时候不能加static
     */
    private static class Node {
        int value; // 值
        Node next; // 下一个节点

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

}

